// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var NetworkManager = require('NetworkManager')
var ClickManager = require('ClickManager')

var scoreItem = cc.Class({
    name: 'scoreItem',
    properties: {
        id: 0,
        tid: 0,
        content: '',
        time: ''
    }
});

cc.Class({
    extends: cc.Component,

    properties: {
        item: {
            default: [],
            type: scoreItem
        },
        itemPrefab: cc.Prefab,
        scoreNode: cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.getScpreData()
    },

    start () {

    },

    getScpreData () {
        var token = cc.sys.localStorage.getItem('__TOKEN__')
        this.scoreNode.removeAllChildren()

        try {
            NetworkManager.onConnectedToHall( (hallService) => {
                hallService.proxy.getAllScore(token, (data) => {
                    var list = JSON.parse(data.datas)
                    cc.log('战绩数据:', list)
                    this.scoreNode.height = list.length
                    for (let i in list) {
                        let obj = {
                            id: list[i].id,
                            tid: list[i].tid,
                            win: list[i].win,
                            innings: list[i].innings,
                            create_time: list[i].create_time
                        }
                        if (obj.win > 0) obj.win = '+'+obj.win

                        var item = cc.instantiate(this.itemPrefab)
                        this.scoreNode.addChild(item)
                        item.getComponent('ScoreItemList').init(obj)
                    }
                })
            })
        } catch (err) {
            console.log('异常处理，待处理:', err)
        }
    }

    // update (dt) {},
});
